import 'package:flutter/material.dart';
import '../res/listData.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Flutter APP", style: TextStyle(color: Colors.white)),
          backgroundColor: Colors.blue,
        ),
        body: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  Widget _initGridViewData(context, i) {
    return Container(
      decoration: BoxDecoration(border: Border.all(color: Colors.black)),
      child: Column(
        children: [
          Image.network(listData[i]["imageUrl"]),
          SizedBox(height: 10),
          Text(listData[i]["author"], style: TextStyle(fontSize: 18)),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      padding: EdgeInsets.all(10),
      itemCount: listData.length,
      gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
        crossAxisSpacing: 10, // 水平子Widget之间的间距
        mainAxisSpacing: 10, // 垂直子Widget之间的间距
        maxCrossAxisExtent: 120, // 子Widget的最大宽度
        // crossAxisCount: 2, // 横轴子元素的数量为2
        childAspectRatio: 0.8, // 子Widget的长宽比
      ),
      itemBuilder: _initGridViewData,
    );
  }
}
